import { defineStore } from 'pinia'
import gzStorageWrapper from '@/utils/gzStorageWrapper'
import {getCurrentUserInfo} from '@/api/login'
import storage from '@/utils/gzStorageWrapper'
export const useUserStore = defineStore('user', {
    state: () => ({
        userInfo: {
            sysUserId: '', // 用户ID
            avatarUrl: '', // 头像
            allMenuRouteTree: [], // 全部动态 router
            entIdList: [] as any, // 用户权限集合
            isAdmin: '', // 是否是超级管理员
            loginUsername: '', // 登录用户名
            state: '', // 用户状态
            sysType: '', // 所属系统
            telphone: '', // 手机号
            sex: '', // 性别
            isHasAgentEnt: false, //是否包含代理商功能
            belongInfoId: '', // 机构号
        },
        globalLoading: false, //全局加载
    }),

    actions: {
        putToken( token:string ) {
            gzStorageWrapper.setToken(token, true)
        },

        refUserInfo(){ // 刷新用户信息
            const that = this

            return getCurrentUserInfo().then(( bizData: any) => {
                this.userInfo = Object.assign({}, bizData)
            })
            
        },

        logout: () => {
            gzStorageWrapper.cleanToken()
            window.location.reload()
            localStorage.clear()
        },
        setGlobalLoading(val){
            this.globalLoading = val
        }
    }

})
